package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.UserAudienceTemplateRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.UserAudienceTemplate;

import java.util.List;

/**
 * Created by Jacky on 2015/1/31.
 */
@Service
public class UserAudienceTemplateRdbServiceImpl extends DaoTemplate implements UserAudienceTemplateRdbService {

    @Override
    public Long saveUserAudienceTemplate(final UserAudienceTemplate template) {
		return saveResultId(template);
    }

	/**
	 * @discription
	 * @author giser.yang       
	 * 创建时间 2015年7月8日 下午2:39:59      
	 * @param productConfigurationId
	 * @param fbConnectionObjectId
	 * @return     
	 * @see com.ndp.fb.rdb.api.UserAudienceTemplateRdbService#(java.lang.Long, java.lang.Long, java.lang.Long)
	 */  
	    
	@Override
	public List<UserAudienceTemplate> findByCompanyIdAndConfigurationAndConnectionObject(
			String userid, Long productConfigurationId,
			Long fbConnectionObjectId) {
		 String sql = "select * from user_audience_template uat where uat.user_id = ? " +
	                "and uat.product_configuration_id = ? and uat.fb_connection_object_id = ? " +
	                "order by create_time desc";
	        return getJdbcTemplate().query(sql, new Object[]{userid, productConfigurationId, fbConnectionObjectId}, DefaultRowMapper.autoRowMapper(UserAudienceTemplate.class));
	}

	/** 
	 * @discription
	 * @author giser.yang       
	 * 创建时间 2015年7月8日 下午3:19:37      
	 * @param fbConnectionObjectId
	 * @param productCfgId
	 * @param name
	 * @return     
	 * @see com.ndp.fb.rdb.api.UserAudienceTemplateRdbService#countByCompanyConnectionObjectProductCfgAndName(java.lang.Long, java.lang.Long, java.lang.Long, java.lang.String)     
	 */  
	    
	@Override
	public Long countByCompanyConnectionObjectProductCfgAndName(Long orgId,
			Long fbConnectionObjectId, Long productCfgId, String name) {
		 String sql = "SELECT COUNT(1) FROM user_audience_template WHERE org_id=? and fb_connection_object_id = ? and product_configuration_id=?  and name=?";
	     return getJdbcTemplate().queryForObject(sql,new Object[]{orgId, fbConnectionObjectId,productCfgId,name}, Long.class);
	}
}
